// 导入组件
import Vue from 'vue';
import VueRouter from 'vue-router';
// 登录
import login from '@/views/login';
// 首页
import index from '@/views/index';
/**
 * 基础菜单
 */
// 商品管理
import Goods from '@/views/goods/Goods';
// 机器信息管理
import Machine from '@/views/machine/Machine';
// 货道信息管理
import MachineAisle from '@/views/machine/MachineAisle';
/**
 * 订单管理
 */
// 交易订单
import Order from '@/views/pay/Order';
/**
 * 系统管理
 */
// 用户管理
import user from '@/views/system/user';
// 菜单管理
import Module from '@/views/system/Module';
// 角色管理
import Role from '@/views/system/Role';
// 公司管理
import Dept from '@/views/system/Dept';
// 系统环境变量
import Variable from '@/views/system/Variable';
// 权限管理
import Permission from '@/views/system/Permission';
/**
 * 支付管理
 */
// 支付配置信息
import MachineConfig from '@/views/machine/MachineConfig';
// 支付配置
import Config from '@/views/pay/Config';
/**
 * 数据监控
 */
// 监控查询
import druidLogin from '@/views/druid/login';

import findTrain from "../views/train/findTrain";

//学员的报名情况
import userSignUp from '@/views/user_train_skc/user_sign_up';

//用户的详情信息
import userTrainMessage from '@/views/user_train_skc/user_train_message.vue';
//用户管理界面
import userManager from '@/views/user_manager_skc/user_manager.vue';
//所有课程信息
import userIndex from '@/views/user_index/user_index.vue';
//用户观看视频
import userCourseLook from '@/views/user_index/user_course_look.vue';

//添加试题
import addTest from "../views/test/addTest";

//查找实体
import findTest from "../views/test/findTest"

import addTrain from "../views/train/addTrain";
//课程管理页面
import courseManage from "../views/course/courseManage";
// 课程管理编辑页
import courseEdit from '@/views/course/courseEdit';
// 课程页面
import course from '@/views/course/course';

//考试界面
import exam from "../views/exam/exam";

//培训目录进度
import catalog_progress from "../views/catalog_progress/catalog_progress";

// //child
// import child from "../views/catalog_progress/child";

import store from "@/store"
import axios from "axios";

// 启用路由
Vue.use(VueRouter);




// 导出路由
const routes = [
  {
    path: '/',
    name: '',
    component: login,
    hidden: true,
    meta: {
      requireAuth: false
    }
  }, {
    path: '/login',
    name: '登录',
    component: login,
    hidden: true,
    meta: {
      requireAuth: false
    }
  }, {
    path: '/index',
    name: '首页',
    component: index,
    iconCls: 'el-icon-tickets',
    children: [
      // //自定义路由views的course课管理页面测试用
      // {
      //   path: '/course',
      //   name: 'course',
      //   component: course,
      //   meta: {
      //     requireAuth: true
      //   }
      // },
      // //自定义路由views的courseManage.vue课管理页面
      // {
      //   path: '/courseManage',
      //   name: 'courseManage',
      //   component: courseManage,
      //   meta: {
      //     requireAuth: true
      //   }
      // },{
      //   path: '/courseEdit/:id',
      //   name: 'courseEdit',
      //   component: courseEdit,
      //   meta: {
      //     requireAuth: true
      //   }
      // },
      // //自定义路由
      // {
      //   path: '/train/findTrain',
      //   name: 'findTrain',
      //   component: findTrain,
      //   meta: {
      //     requireAuth: false
      //   }
      // },
      // {
      //   path: '/train/addTrain',
      //   name: 'addTrain',
      //   component: addTrain,
      //   meta: {
      //     requireAuth: false
      //   }
      // },
      // {
      //   path: '/exam/exam',
      //   name: 'exam',
      //   component: exam,
      //   meta: {
      //     requireAuth: false
      //   }
      // },
      // {
      //   path: '/test/addTest',
      //   name: 'addTest',
      //   component: addTest,
      //   meta: {
      //     requireAuth: false
      //   }
      // },

     // },



    ]
  }]

//解决路由跳转报错
//Error: Navigation cancelled from "/user_index/user_index" to "/user_index/1-
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

//使用路由
const router = new VueRouter({
  routes
})

//动态路由设置
router.beforeEach((to, form, next) => {

  let hasRoute = store.state.menus.hasRoute
  console.log("是否加载", hasRoute)
  let token = localStorage.getItem("Authorization")
  console.log("token", token);
  if (to.path == '/login') {
    console.log("login!!!!!!!!!!!")
    next()
  } else if (token == '' || token == null) {
    console.log("还没有token!!!!!!!!")
    next({ path: "/login" })
  } else if (to.path == '/' || to.path == '') {
    next({ path: "/index" })
  } else if (!hasRoute) {
    axios.get("/api/menu/findMenu", {
      headers: {
        token: localStorage.getItem("Authorization")
      }
    }).then(res => {
      //   console.log(res);
      console.log("返回的数据", res.data.data)

      //拿到listmenu
      store.commit("setMenuList", res.data.data)

      let newRoutes = router.options.routes
      res.data.data.forEach(menu => {
        if (menu.children) {
          menu.children.forEach(e => {

            //转成路由
            let route = menuToRoute(e)
            // console.log("返回的路由", route)
            //路由不为空 把路由添加到路由管理中
            if (route) {
              //  console.log(newRoutes[2]);
              newRoutes[2].children.push(route)
            }
          })
        }
      })

      //绑定路由
      // console.log("newRoutes")
      // console.log(newRoutes)
      router.addRoutes(newRoutes)
      // hasRoute = true
      store.commit("changeRouteStatus", true)

    }).catch(e => {
      console.log(e);


    })
  }
  else {
    console.log("已经有路由了------------")
    next()
  }
  next()

})

//处理每一个路由
const menuToRoute = (menu) => {
  // console.log("正在添加menu--》")
  //console.log(menu)
  if (!menu.component) {
    return null
  }
  // 复制属性
  let route = {
    name: menu.name,
    path: menu.path,
    meta: {
      requireAu: menu.icon,
      title: menu.title,
      requireAuth: menu.requireAuth
    }
  }
  route.component = () => import('@/views/' + menu.component + '.vue')
  return route
}


//暴露路由
export default router
